<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="标题" prop="newsTitle">
				<el-input v-model="form.infoTitle" placeholder="标题" maxlength="80" clearable></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="内容类型" prop="newsType">
						<sc-select v-model="form.newsType" :params="{code:'newsType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="是否置顶" prop="isTop">
						<el-switch v-model="form.isTop" active-value="1" inactive-value="0"></el-switch>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="专题专栏" prop="topicId">
						<sc-select v-model="form.topicId" :params="{parentId:'0'}" :apiObj="$API.topic.topicDic.getTopicDicChildList" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12"> 
					<el-form-item label="栏目名称" prop="columnId">
						
						<!-- <sc-select v-model="form.columnId" :params="{parentId:form.topicId}" :apiObj="$API.topic.topicDic.getTopicDicChildList" clearable filterable style="width: 100%"></sc-select> -->
						<sc-select v-model="form.columnId" :params="{code:form.topicId}" :apiObj="$API.topic.topicDic.getTopicDicByCode" clearable filterable style="width: 100%"></sc-select> 
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="人员权限" prop="userRole">
				<select-user-dialog v-model:accountId="form.userRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="部门权限">
				<select-dept-dialog v-model:deptId="form.deptRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="职务权限">
				<select-user-level-dialog v-model:levelId="form.levelRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="专题内容">
				<sc-editor v-model="form.content" placeholder="请输入" :templates="templates" :height="400" clearable  style="width: 100%;"></sc-editor>
			</el-form-item>
			<!--
				<msg-box-check-box v-model:msgType="form.msgType" code="news"></msg-box-check-box>
			-->
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">{{ btName }}</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import {defineAsyncComponent} from "vue";
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserLevelDialog,
		msgBoxCheckBox,
		selectUserDialog,
		selectDeptDialog,
		scEditor
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			btName: "保存",
			titleMap: {
				add: '发布专题内容',
				edit: '编辑新闻',
			},
			templates: [],
			form: {
				infoId:'',
				infoTitle: '',
				newsType: '',
				isTop: '0',
				userRole: '',
				deptRole: '',
				levelRole: '',
				content:'',
				msgType:'',
				topicId:'',
				columnId:''
			},
			rules: {
				infoTitle: [
					{required: true, message: '标题不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			if (this.mode == "add") {
				this.btName = "发布";
			} else {
				this.btName = "保存";
			}
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.topic.info.insert.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.topic.info.update.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.infoId = data.infoId
			this.form.infoTitle = data.infoTitle
			this.form.newsType = data.newsType
			this.form.isTop = data.isTop
			this.form.userRole = data.userRole
			this.form.deptRole = data.deptRole
			this.form.levelRole = data.levelRole
			this.form.content = data.content
			this.form.msgType = data.msgType
			this.form.topicId = data.topicId
			this.form.columnId = data.columnId
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
